﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace yafc.UI.Components
{
	public partial class PostItem : ListBoxItem
	{
		private Post post;

		public Post Post
		{
			get { return post; }
		}

		protected Font NameFont
		{
			get
			{
				return ThemeManager.GetCurrentTheme().GetFont("NameFont");
			}
		}
		protected Font TimeFont
		{
			get
			{
				return ThemeManager.GetCurrentTheme().GetFont("TimeFont");
			}
		}
		protected Font TextFont
		{
			get
			{
				return ThemeManager.GetCurrentTheme().GetFont("TextFont");
			}
		}

		protected Brush NameBrush
		{
			get
			{
				return ThemeManager.GetCurrentTheme().GetBrush("NameBrush");
			}
		}
		protected Brush TimeBrush
		{
			get
			{
				return ThemeManager.GetCurrentTheme().GetBrush("TimeBrush");
			}
		}
		protected Brush TextBrush
		{
			get
			{
				return ThemeManager.GetCurrentTheme().GetBrush("TextBrush");
			}
		}
		protected Brush BgBrush
		{
			get
			{
				if (!Selected)
				{
					return ThemeManager.GetCurrentTheme().GetBrush("BgBrush");
				}
				else
				{
					return ThemeManager.GetCurrentTheme().GetBrush("HighlightBgBrush");
				}
			}
		}

		public int UpEdge
		{
			get { return 5; }
		}
		public int DownEdge
		{
			get { return 5; }
		}
		public int LeftEdge
		{
			get { return 3; }
		}
		public int RightEdge
		{
			get { return 3; }
		}

		public PostItem(Post p)
		{
			InitializeComponent();


			post = p;
		}

		private void PostItem_Paint(object sender, PaintEventArgs e)
		{
			var g = e.Graphics;
			var bounds = e.ClipRectangle;

			bounds.Location = new Point(0, 0);
			bounds.Size = Size;

			DrawBg(g, bounds);

			string timestamp = post.timestamp.ToShortDateString() + " " + post.timestamp.ToShortTimeString();
			SizeF namesize = g.MeasureString(post.name, NameFont);
			SizeF timesize = g.MeasureString(timestamp, TimeFont);

			Point clientLeftTop = new Point(bounds.X + LeftEdge, bounds.Y + UpEdge);
			Rectangle clientRect = new Rectangle(clientLeftTop, ClientSize);
			g.DrawString(post.name, NameFont, NameBrush, clientRect);
			g.DrawString(timestamp, TimeFont, TimeBrush, clientRect.X + clientRect.Width - timesize.Width, clientRect.Y);

			DrawText(g, bounds.X, clientLeftTop.Y + namesize.Height);
		}
		protected virtual void DrawBg(Graphics g, Rectangle bounds)
		{
			g.FillRectangle(BgBrush, bounds);
		}
		protected virtual void DrawSelectedRectangle(Graphics g, Rectangle bounds)
		{
			int x1 = bounds.X;
			int y1 = bounds.Y;
			int x2 = bounds.X + bounds.Width;
			int y2 = bounds.Y + bounds.Height;

			int minEdge = LeftEdge;

			using (var pen = new Pen(BgBrush, minEdge))
			{
				g.DrawLine(pen, new Point(x1, y1 + minEdge / 2), new Point(x2, y1 + minEdge / 2));

				g.DrawLine(pen, new Point(x1, y2 - minEdge / 2), new Point(x2, y2 - minEdge / 2));

				g.DrawLine(pen, new Point(x1 + minEdge / 2, y1), new Point(x1 + minEdge / 2, y2));

				g.DrawLine(pen, new Point(x2 - minEdge / 2, y1), new Point(x2 - minEdge / 2, y2));
			}
		}
		protected virtual void DrawText(Graphics g, float X, float Y)
		{
			PointF leftup = new PointF(X + LeftEdge, Y + RightEdge);

			g.DrawString(post.text, TextFont, TextBrush,
				new RectangleF(leftup, ClientSize), StringFormat.GenericDefault);

			//StringBuilder sbText2Draw(post.text);
			//string string2Draw;
			//int linebreak;
			//int linewrap;
			//while (sbText2Draw.Length > 0)
			//{
			//    linebreak = sbText2Draw.ToString().IndexOf('\n');
			//    g.MeasureString(sbText2Draw.ToString(), TextFont, ClientWidth, StringFormat.GenericDefault,
			//        out )
			//}
		}

		private void PostItem_Layout(object sender, LayoutEventArgs e)
		{
			Width = Parent.ClientSize.Width;

			using (var g = Parent.CreateGraphics())
			{
				SizeF textsize = g.MeasureString(Post.text, TextFont, ClientSize.Width - LeftEdge - RightEdge);
				Height = Math.Max(NameFont.Height, TimeFont.Height) + (int)Math.Ceiling(textsize.Height) + UpEdge + DownEdge;
			}
			Invalidate();
		}
	}
}
